// pages/orderDetail/orderDetail.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      orderInfo:null,
      payType:1,
    inputData: {
      input_value: "",//输入框的初始内容
      value_length: 0,//输入框密码位数
      isNext: false,//是否有下一步的按钮
      get_focus: true,//输入框的聚焦状态
      focus_class: true,//输入框聚焦样式
      value_num: [1, 2, 3, 4, 5, 6],//输入框格子数
      height: "60rpx",//输入框高度
      width: "400rpx",//输入框宽度
      see: false,//是否明文展示
      interval: true,//是否显示间隔格子
      showpaw:false,
      userInfo:null,
      transMes:null,
      showtrans:false,
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        let id=options.id;
        let self=this;
        wx.request({
          url:app.globalData.reqPath+'shop/orders/'+id,
          header:{
            'authorization': app.globalData.authorization
          },
          success:function({data}){
              if(data.code==200){
                self.setData({orderInfo:data.data})
              }
          }
        })
    //获取个人信息
    wx.request({
      url: app.globalData.reqPath + 'shop/Members',
      data: {},
      header: {
        'authorization': app.globalData.authorization
      },
      success: function ({ data, header }) {
        if (data.code == 200) {
          self.setData({ userInfo: data.data })
        }
      }
    })
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changePayway: function (e) {
    this.setData({ payType: e.detail.value })
  },
  cancelorder: function (e) {
    let id = this.data.orderInfo.id;
    let self = this;
    wx.showModal({
      content: '确定取消订单？',
      success: function (res) {
        if (res.confirm) {
          //console.log('用户点击确定')
          wx.request({
            url: app.globalData.reqPath + 'shop/OrdersStatusCancle',
            method: 'POST',
            header: {
              authorization: app.globalData.authorization
            },
            data: {
              orderId: id
            },
            success: function ({ data }) {
              if (data.code == 200) {
                wx.showToast({
                  title: '取消成功',
                  icon: 'none',
                  success: function () {
                    self.onLoad({ id: self.data.orderInfo.id});
                  }
                })
              }
            }
          })
        }
      }
    })
  },
  cofrimpay:function(){
    let data = { 
      orderId: this.data.orderInfo.id,
      payType: this.data.payType
    }
    let self=this;
let way = this.data.payType;
    if (way == 1) {
      let userInfo = this.data.userInfo;
      if (userInfo.tradePasswordSet == 0) {
        wx.showModal({
          content: '没有支付密码，去设置',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/setpaw/setpaw?r=1&orderid='+self.data.orderInfo.id,
              })
            }
          }
        })
      } else {
        this.setData({ showpaw: true })
      }
    } else {  
      wx.request({
        url: app.globalData.reqPath + 'shop/OrdersStatusPay',
        method: 'POST',
        header: {
          'authorization': app.globalData.authorization
        },
        data: {
          orderId: this.data.orderInfo.id,
          payType: 5,
        },
        success: function ({ data }) {
          console.log(data)
          let wxpaydata = data.data;
          if (data.code == 200) {//调起支付
            wx.requestPayment({
              timeStamp: wxpaydata.timeStamp,
              nonceStr: wxpaydata.nonceStr,
              package: wxpaydata.package,
              signType: wxpaydata.signType,
              paySign: wxpaydata.paySign,
              success(res) {
                console.log(res)
                if (res.errMsg == "requestPayment:ok") {
                  wx.redirectTo({
                    url: '/pages/order/order?s=2',
                  })
                }
              },
              fail(res) {
                if (res.errMsg == 'requestPayment:fail cancel') {//res.errMsg //requestPayment: fail cancel //用户取消支付
                  wx.redirectTo({
                    url: '/pages/order/order?s=0',
                  })
                } else { //requestPayment: fail(detail message)//调用支付失败
                  wx.showToast({
                    title: '支付失败，请重试',
                    icon: 'none'
                  })
                }
              }
            })
          } else {
            wx.showToast({
              title: res.data.data.msg,
              icon: 'none'
            })
          }
        }
      }) 
    }  
  },
  comfirmpawPay: function (e) {
    let paw = e.detail.value.paw;
    let self = this;
    wx.request({
      url: app.globalData.reqPath + 'shop/OrdersStatusPay',
      data: {
        orderId: this.data.orderInfo.id,
        payType: 1,
        tradePassword: paw
      },
      header: {
        'authorization': app.globalData.authorization
      },
      method: 'POST',
      success: function ({ data }) {
        console.log(data)
        if (data.code == 200) {
          console.log(data)
          wx.redirectTo({
            url: '/pages/order/order?s=2',
          })
        } else {
          wx.showToast({
            title: data.data.msg,
            icon: 'none',
            success: function () {
              self.onLoad({id:self.data.orderInfo.id});
            }
          })
        }
      }
    })
  },
  onClosepaw:function(){
    this.setData({showpaw:false});
  },
  cfmget:function(){//确认收货
    let self = this;
    let id=this.data.orderInfo.id;
    wx.showModal({
      title: '是否确认收到货？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.reqPath + 'shop/OrdersStatusExpress',
            method: 'POST',
            data: {
              orderId: id
            },
            header: {
              'authorization': app.globalData.authorization
            },
            success: function ({ data }) {
              if (data.code == 200) {
                wx.redirectTo({
                  url: '/pages/order/order?s=4',
                })
              }
            }
          })
        }
      }
    })
  },
  transMes:function(){//查看物流
    let id = this.data.orderInfo.id;
    let self = this;
    this.setData({ showtrans: true })
    wx.request({
      url: app.globalData.reqPath + 'shop/express',
      data: {
        orderId: id
      },
      header: {
        authorization: app.globalData.authorization
      },
      success: function ({ data }) {
        if (data.code == 200) {
          self.setData({ transMes: data.data })
        }
      }
    })
  },
  onClosetrans:function(){
    this.setData({ showtrans: false })
  },
  torate:function(){//去评价
    let id = this.data.orderInfo.id;
    wx.navigateTo({
      url: '/pages/subrate/subrate?id=' + id,
    })
  },
  return1:function(){//申请退款
    let id = this.data.orderInfo.id;
    wx.navigateTo({
      url: '/pages/return/return?id=' + id + '&type=2',
    })
  },
  return2: function () {//申请退货
    let id = this.data.orderInfo.id;
    wx.navigateTo({
      url: '/pages/return/return?id=' + id + '&type=1',
    })
  }
})